﻿using B.Auth.APi.Application.Command.Users;
using B.Auth.Domain.Entity;
using B.Auth.Domain.Repository;
using B.Auth.Enum;
using MediatR;

namespace B.Auth.APi.Application.CommandHand
{
    public class AddUserCommandHand : IRequestHandler<AddUserCommand, ApiResult<int>>
    {

        private readonly IBaseRepository<User> db;

        public AddUserCommandHand(IBaseRepository<User> db)
        {
            this.db = db;
        }

        public Task<ApiResult<int>> Handle(AddUserCommand request, CancellationToken cancellationToken)
        {
           var result=new ApiResult<int>();
           
            var user=new User();    
            user.Name = request.Name;
            user.Sex=request.Sex;
            user.FromProvince = request.FromProvince;
            user.FromCity=request.FromCity;
            user.FromCounty=request.FromCounty;
            user.Addressmessage=request.Addressmessage;
            user.CreateUserName = "张三";
            user.CreateTime=DateTime.Now;
            user.UpdateTime = null;
            user.UodateUserName ="";

            user.PassWord = request.PassWord;
            user.UserName = request.UserName;
            user.departid=request.departid;

            user.Del = 0;

            var useshow = db.GetAll().Any(x => x.Name ==request.Name&&x.Del==0);
            if (useshow)
            {

                result.Message = "用户名不能重复";
                result.Code = ApiEnum.fail;
                return Task.FromResult(result);

            }
            db.Add(user);
            result.Message = "添加成功";
            result.Code = ApiEnum.ok;
          

            return Task.FromResult(result);
            

        }
    }
}
